const express = require('express');
const router = express.Router();
const SheetMusic = require('../models/SheetMusic');
const auth = require('../middleware/auth');

// Get all public sheet music
router.get('/public', async (req, res) => {
  try {
    const sheetMusic = await SheetMusic.find({ isPublic: true })
      .populate('userId', 'username')
      .sort({ createdAt: -1 });
    res.json(sheetMusic);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// Get user's sheet music
router.get('/my-sheets', auth, async (req, res) => {
  try {
    const sheetMusic = await SheetMusic.find({ userId: req.user._id })
      .sort({ updatedAt: -1 });
    res.json(sheetMusic);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// Get single sheet music
router.get('/:id', async (req, res) => {
  try {
    const sheetMusic = await SheetMusic.findById(req.params.id)
      .populate('userId', 'username');
    
    if (!sheetMusic) {
      return res.status(404).json({ message: 'Sheet music not found' });
    }
    
    if (!sheetMusic.isPublic && (!req.user || sheetMusic.userId._id.toString() !== req.user._id.toString())) {
      return res.status(403).json({ message: 'Not authorized' });
    }
    
    res.json(sheetMusic);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// Create new sheet music
router.post('/', auth, async (req, res) => {
  const sheetMusic = new SheetMusic({
    title: req.body.title,
    artist: req.body.artist,
    content: req.body.content,
    userId: req.user._id,
    isPublic: req.body.isPublic || false
  });

  try {
    const newSheetMusic = await sheetMusic.save();
    res.status(201).json(newSheetMusic);
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
});

// Update sheet music
router.patch('/:id', auth, async (req, res) => {
  try {
    const sheetMusic = await SheetMusic.findById(req.params.id);
    
    if (!sheetMusic) {
      return res.status(404).json({ message: 'Sheet music not found' });
    }
    
    if (sheetMusic.userId.toString() !== req.user._id.toString()) {
      return res.status(403).json({ message: 'Not authorized' });
    }
    
    Object.assign(sheetMusic, req.body);
    const updatedSheetMusic = await sheetMusic.save();
    res.json(updatedSheetMusic);
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
});

// Delete sheet music
router.delete('/:id', auth, async (req, res) => {
  try {
    const sheetMusic = await SheetMusic.findById(req.params.id);
    
    if (!sheetMusic) {
      return res.status(404).json({ message: 'Sheet music not found' });
    }
    
    if (sheetMusic.userId.toString() !== req.user._id.toString()) {
      return res.status(403).json({ message: 'Not authorized' });
    }
    
    await sheetMusic.remove();
    res.json({ message: 'Sheet music deleted' });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

module.exports = router; 